package com.landicorp.android.skkp.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.landicorp.android.skkp.R;
import com.landicorp.android.skkp.model.InvoiceTitleInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zc on 2016-12-29.
 */

public class InvoiceTitleInfoAdapter extends RecyclerView.Adapter<InvoiceTitleInfoAdapter.InvoiceTitleInfoHolder> {

    private List<InvoiceTitleInfo> mInvoiceTitleList = new ArrayList<>();
    private Listener mListener;

    public interface Listener {
        void onItemClicked(InvoiceTitleInfo info);
    }

    public InvoiceTitleInfoAdapter(Listener listener) {
        mListener = listener;
    }

    public InvoiceTitleInfoAdapter(Listener listener, List<InvoiceTitleInfo> list) {
        mListener = listener;
        mInvoiceTitleList.clear();
        mInvoiceTitleList.addAll(list);
    }

    @Override
    public InvoiceTitleInfoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.item_invoice_title_info, parent, false);
        return new InvoiceTitleInfoHolder(view);
    }

    @Override
    public void onBindViewHolder(InvoiceTitleInfoHolder holder, int position) {
        InvoiceTitleInfo info = mInvoiceTitleList.get(position);
        holder.textViewHeader.setText(info.getName());
        //最后一条数据，不显示分割线
        if (position == getItemCount() - 1) {
            holder.footerDivider.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mInvoiceTitleList != null ? mInvoiceTitleList.size() : 0;
    }

    public class InvoiceTitleInfoHolder extends RecyclerView.ViewHolder {

        public TextView textViewHeader;
        public View footerDivider;

        public InvoiceTitleInfoHolder(View itemView) {
            super(itemView);

            textViewHeader = (TextView) itemView.findViewById(R.id.text_view_header);
            footerDivider = (View) itemView.findViewById(R.id.footer_divider);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = getAdapterPosition();
                    if (mListener != null && adapterPosition != RecyclerView.NO_POSITION) {
                        mListener.onItemClicked(mInvoiceTitleList.get(adapterPosition));
                    }
                }
            });
        }
    }

    public void swapData(List<? extends InvoiceTitleInfo> titleList) {
        mInvoiceTitleList.clear();
        mInvoiceTitleList.addAll(titleList);
        notifyDataSetChanged();
    }
}
